<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="__LAYUI__/layui.js"></script>
    <link href="__LAYUI__/css/layui.css" rel="stylesheet"/>
</head>
<body>

<table id="file" lay-filter="text"></table>

<div id="upload">


</div>


<script type="text/html" id="uploader">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>文件上传</legend>
    </fieldset>
    <fieldset class="layui-elem-field">
        <legend>文件名称</legend>
        <div class="layui-field-box">
            <label id="lab"></label>
        </div>
    </fieldset>

    <div class="layui-upload-drag" id="test10">
        <i class="layui-icon"></i>
        <p>点击上传，或将文件拖拽到此处</p>
        <p>文件格式：.doc/.docx/.txt</p>
    </div>
</script>


<script type="text/html" id="fileName">
    <a href="{{d.url}}" download="" class="layui-table-link">{{d.name}}</a>
</script>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
</script>

<script>
    layui.use(['table','upload','jquery'],function () {
        var upload = layui.upload
            ,$=layui.$
            ,table = layui.table;
        if({$level}!='1'){
            $('#upload').html($('#uploader').html());
        }

        if({$level}=='0'){
            table.render({
                elem:'#file',
                height:340,
                cols:[[
                    {field:'Id',title:'id',width:100},
                    {field:'file',title:'文件',width:500,templet:'#fileName'},
                    {field:'date',title:'上传日期',width:200},
                    {fixed: 'right', width: 160, align: 'center', toolbar: '#barDemo'}
                ]],
                url:'/index/file/getFiles',
                page:true,
                limit:10,
                response:{
                    statusCode:1
                }
                ,id:'file'
            });
        }else if({$level}!='1'){
            table.render({
                elem:'#file',
                height:220,
                cols:[[
                    {field:'Id',title:'id',width:100},
                    {field:'file',title:'文件',width:500,templet:'#fileName'},
                    {field:'date',title:'上传日期',width:200}
                ]],
                url:'/index/file/getFiles',
                page:true,
                limit:10,
                response:{
                    statusCode:1
                }
                ,id:'file'
            });
        }else{
            table.render({
                elem:'#file',
                height:520,
                cols:[[
                    {field:'Id',title:'id',width:100},
                    {field:'file',title:'文件',width:500,templet:'#fileName'},
                    {field:'date',title:'上传日期',width:200}
                ]],
                url:'/index/file/getFiles',
                page:true,
                limit:10,
                response:{
                    statusCode:1
                }
                ,id:'file'
            });
        }

        table.on('tool(text)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.open({
                    type:0 ,
                    offset:'30px',
                    content:'确定删除?',
                    yes:function (index, layero) {
                        $.ajax({
                            type:'post',
                            url:'/index/uploader/delFile',
                            data:{
                                'file_id':data.Id
                            },
                            success:function (result) {
                                if(result.code){
                                    layer.msg(result.msg, {
                                        icon: 1,
                                        time: 3000 //2秒关闭（如果不配置，默认是3秒）
                                    });
                                }
                                table.reload('file',{
                                });

                            }
                        });

                        layer.close(index);
                    }

                });
            }
        });



        upload.render({
            elem: '#test10'
            ,url: '/index/uploader/upload'
            ,exts:'doc|docx|txt'
            ,accept:'file'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#file_name').val(file.name)
                    $('#lab').html(file.name); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res)
                $.ajax({
                    type: 'post'
                    ,url:'/index/uploader/saveFile'
                    ,data:{'name':$('#lab').html(),'id':res.data.id}
                    ,success:function () {
                        layer.msg('上传成功！', {
                            icon: 1,
                            time: 3000
                        });
                        table.reload('file',{});
                    }
                })
            }
        });
    })
</script>

</body>
</html>